<?php

/**
 * Plugin bahnde: Search-Form for Bahn.de
 * 
 * @license    GNU
 * @author     Juergen A.Lamers <jaloma.ac@googlemail.com>
 */

if (!defined('DOKU_INC'))
	define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
if (!defined('DOKU_PLUGIN'))
	define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once (DOKU_PLUGIN . 'syntax.php');

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */

class syntax_plugin_bahnde extends DokuWiki_Syntax_Plugin {

  var $dflt = array(
  'start_station' => '',
  'end_station' => '',
  'start_date' => '',
  'start_time' =>'',
  'allow_toggle_station' => false,
  'departure' => '',
);

	/**
	 * return some info
	 */
	function getInfo() {
		return array (
			'author' => 'Juergen A.Lamers',
			'email' => 'jaloma.ac@googlemail.com',
			'date' => '2008-12-01',
			'name' => 'bahnde -- www.reiseauskunft.de Plugin',
			'desc' => 'Bahn.de Formular for your Site',
			'url' => 'http://wiki.dokuwiki.org/plugin:bahnde',
		);
	}

	/**
	 * What kind of syntax are we?
	 */
	function getType() {
		return 'substition';
	}

	/**
	 * What about paragraphs? (optional)
	 */
	function getPType() {
		return 'block';
	}

	/**
	 * Where to sort in?
	 */
	function getSort() {
		return 999;
	}

	/**
	 * Connect pattern to lexer
	 */
	function connectTo($mode) {
		$this->Lexer->addSpecialPattern('<bahnde.*?/>', $mode, 'plugin_bahnde');
	}

	function matchLength() {
		return strlen("<bahnde");
	}

	/**
	 * Handle the match
	 */
	function handle($match, $state, $pos, & $handler) {
    	$match = html_entity_decode(substr($match,$this->matchLength(),-2));
    	$gmap = $this->_extract_params($match);
    	return $gmap;
	}

  /**
   * extract parameters for the googlemap from the parameter string
   *
   * @param   string    $str_params   string of key="value" pairs
   * @return  array                   associative array of parameters key=>value
   */
  function _extract_params($str_params) {
    $param = array();
    preg_match_all('/(\w*)="(.*?)"/us',$str_params,$param,PREG_SET_ORDER);
    if (sizeof($param) == 0) {
      preg_match_all("/(\w*)='(.*?)'/us",$str_params,$param,PREG_SET_ORDER);
    }
    // parse match for instructions, break into key value pairs      
    $gmap = $this->dflt;
    foreach($param as $kvpair) {
      list($match,$key,$val) = $kvpair;
//      $key = strtolower($key);
      if (isset($gmap[$key])) $gmap[$key] = $val;        
    }

    return $gmap;
  }

	/**
	 * Create output
	 */
	function render($mode, & $renderer, $data) {
		if ($mode == 'xhtml') {
		  $start_station = $data['start_station'];
		  $end_station = $data['end_station'];
		  $start_date = $data['start_date'];
		  $start_time = $data['start_time'];
		  $departure = $data['departure'];
		  $allow_toggle_station = $data['allow_toggle_station'];
		  $txt = $this->bahnDeDiv($end_station, $start_date, $start_time, $departure, $allow_toggle_station, $start_station);
		  $renderer->doc .= $txt;
		  return true;
		}
		return false;
	}
	
	function bahnDeDiv($end_station, $start_date, $start_time, $departure, $allow_toggle_station, $start_station) {
//		$txt = '++'.$start_station.'--';
		$txt = '';
		$txt .= '<div style="background-image:url('."'http://www.db.de/site/bahn/layout,property=LOGO__PROVIDER__IMG.gif'".');background-repeat:no-repeat;background-position:bottom left;width=200px">';
$txt .= '		<form accept-charset="ISO-8859-1" action="http://reiseauskunft.bahn.de/bin/query.exe/dn" name="auskunft" method="post" id="auskunft" target="_blank">';

$txt .= '			<input type="hidden" name="start" value="1" />';
if ($start_date != null) {
$txt .= '<input type="hidden" name="returnTime" value="'.$start_date.'" />';
}
$txt .= '			<input type="hidden" name="returnTimesel" value="depart" />';
$txt .= '			<input type="hidden" name="REQ0Tariff_TravellerType.1" value="E"  />';
$txt .= '			<input type="hidden" name="REQ0Tariff_TravellerReductionClass.1" value="0"  />';
$txt .= '			<input type="hidden" name="REQ0Tariff_Class" value="2"  />';
$txt .= '			<input type="hidden" name="REQ0JourneyProduct_prod_list" id="REQ0JourneyProduct_prod_list" value="1:1111111111000000"  />';
	  
$txt .= '			<table cellpadding="0" cellspacing="0" border="0">';
$txt .= '			<tr><td>';

$txt .= '				<table cellspacing="0" cellpadding="1" style="margin-top:3px;margin-bottom:3px;">';
$txt .= '					<tr>';
$txt .= '			            <td>';
$txt .= '							<a href="#" id="bf" title="Geben Sie eine Station ein, um hier einen Abfahrtsplan zu &ouml;ffnen"><label for="S">Von:</label><br />';
$txt .= '						</td>';

$txt .= '						<td colspan="2">';
if ($start_station != null) {
$txt .= '							<input id="S" name="S" type="hidden" size="18" value="'.$start_station.'"/>'.$start_station.'<br />';
} else {
$txt .= '							<input id="S" name="S" type="text" onKeyUp="updateurl();" size="18" /><br />';
}
$txt .= '						</td>';

$txt .= '					</tr>';
$txt .= '					<tr>';
$txt .= '						<td>';
$txt .= '							<label for="Z">Nach:</label><br />';
$txt .= '						</td>';
$txt .= '						<td colspan="2">';
if ($end_station != null ) {
$txt .= '							<input id="Z" name="Z" type="hidden" size="18" value="'.$end_station.'"/>'.$end_station.'<br />';
	
} else {
$txt .= '							<input id="Z" name="Z" type="text" size="18" />&nbsp;<br />';
}
$txt .= '						</td>';
$txt .= '					</tr>';
$txt .= '					<tr>';
$txt .= '						<td>';
$txt .= '							<label for="date">Datum:</label>';
$txt .= '						</td>';
$txt .= '						<td>';
if ($start_date != null) {
$txt .= '							<input id="date" name="date" type="hidden" maxlength="10" size="6" value="'.$start_date.'"/>'.$start_date;	
} else {
$txt .= '							<input id="date" name="date" type="text" maxlength="10" size="6" />';
}
$txt .= '						</td>';
$txt .= '						<td rowspan="2">';
if ($departure != '') {
	if (($departure == true || $departure == "true")) {
		$time_prefix = "Abfahrtszeit";
		$txt .= '							<input type="hidden" checked="1" name="timesel" value="depart" id="T1" />';
	}	
	if (($departure == false || $departure=="false")) {
		$time_prefix = "Ankunftszeit";
		$txt .= '							<input type="hidden" checked="1" name="timesel" value="arrive" id="T2" />';
	}
} else {
  	$txt .= '							<label for="T1">Abfahrt</label><br />';
  	$txt .= '							<input type="radio" name="timesel" value="depart" id="T1" checked="1"/>';
	$txt .= '							<label for="T2">Ankunft</label>';
	$txt .= '							<input type="radio" name="timesel" value="arrive" id="T2" />';
}
$txt .= '						</td>';
$txt .= '					</tr>';
$txt .= '					<tr>';
$txt .= '						<td>';
if ($time_prefix != null) {
$txt .= '							<label for="time">'.$time_prefix.':&nbsp;</label><br />';	
} else {
$txt .= '							<label for="time">Uhrzeit:&nbsp;</label><br />';
}
$txt .= '						</td>';
$txt .= '						<td>';
if ($start_time != null) {
$txt .= '							<input id="time" name="time" type="hidden" maxlength="5" size="6" value="'.$start_time.'"/>'.$start_time;
	
} else {
$txt .= '							<input id="time" name="time" type="text" maxlength="5" size="6" />';
}
$txt .= '						</td>';
$txt .= '					</tr>';
$txt .= '					<tr>';
$txt .= '						<td colspan="3">';
$txt .= '							<input name="submitButton" value="Verbindung suchen" class="submit" alt="Submit" type="submit" />';
$txt .= '						</td>';
$txt .= '					</tr>';
$txt .= '				</table>';

$txt .= '			</td>';
if ($allow_toggle_station == 'true') {
$txt.='<td valign="top">';
$txt .= '				<table class="tables" cellpadding="0" cellspacing="0" border="0">';
$txt .= '					<tr>';
$txt .= '						<td>';
$txt .= '							<input type="button" class="switch" onClick="flipStations();" value=" " title="Stationen tauschen" />';
$txt .= '							<img onClick="flipStations();" src="http://projects.ff22.de/gadgets/flip.gif" title="Stationen tauschen" class="imgs" />';
$txt .= '						</td>';
$txt .= '					</tr>';
$txt .= '				</table>';
$txt .= '			</td>';
}
$txt .= '			</tr>';
$txt .= '			</table>';

$txt .= '		</form>';
$txt .= '	</div>';
		return $txt;
	}
} // Class


//Setup VIM: ex: et ts=4 :